/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Pluginsclassify;
import me.zhengjie.modules.system.service.PluginsclassifyService;
import me.zhengjie.modules.system.domain.vo.PluginsclassifyQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author shitou
* @date 2024-04-22
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "组件分类管理")
@RequestMapping("/api/pluginsclassify")
public class PluginsclassifyController {

    private final PluginsclassifyService pluginsclassifyService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    // @PreAuthorize("@el.check('pluginsclassify:list')")
    public void exportPluginsclassify(HttpServletResponse response, PluginsclassifyQueryCriteria criteria) throws IOException {
        pluginsclassifyService.download(pluginsclassifyService.queryAll(criteria), response);
    }



    @GetMapping("/querypluginclassify")
    @Log("查询组件分类不分页1")
    @ApiOperation("查询组件分类1")
    public ResponseEntity <List<Pluginsclassify>> queryPluginsclassifyTree(PluginsclassifyQueryCriteria criteria){
        List<Pluginsclassify> pluginsclassifyList = pluginsclassifyService.queryAll(criteria);
        List<Pluginsclassify> list1 = new ArrayList<>();
        List<Pluginsclassify> list2 = new ArrayList<>();
        for(Pluginsclassify pluginsclassify:pluginsclassifyList){
            if(pluginsclassify.getRootid() == 0 || pluginsclassify.getRootid() == null){
                list1.add(pluginsclassify);
            } else {
                list2.add(pluginsclassify);
            }
        }


        for(Pluginsclassify pluginsclassify1:list1){
            List<Pluginsclassify> list3 = new ArrayList<>();
            for(Pluginsclassify pluginsclassify2:list2){
                if(pluginsclassify1.getId() == pluginsclassify2.getRootid()){
                    list3.add(pluginsclassify2);
                }
            }

            pluginsclassify1.setChildren(list3);
        }

        return new ResponseEntity<>(list1,HttpStatus.OK);
    }

    @GetMapping
    @Log("查询组件分类")
    @ApiOperation("查询组件分类")
    // @PreAuthorize("@el.check('pluginsclassify:list')")
    public ResponseEntity<PageResult<Pluginsclassify>> queryPluginsclassify(PluginsclassifyQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(pluginsclassifyService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增组件分类")
    @ApiOperation("新增组件分类")
    // @PreAuthorize("@el.check('pluginsclassify:add')")
    public ResponseEntity<Object> createPluginsclassify(@Validated @RequestBody Pluginsclassify resources){
        pluginsclassifyService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改组件分类")
    @ApiOperation("修改组件分类")
//    @PreAuthorize("@el.check('pluginsclassify:edit')")
    public ResponseEntity<Object> updatePluginsclassify(@Validated @RequestBody Pluginsclassify resources){
        pluginsclassifyService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除组件分类")
    @ApiOperation("删除组件分类")
//    @PreAuthorize("@el.check('pluginsclassify:del')")
    public ResponseEntity<Object> deletePluginsclassify(@RequestBody List<Integer> ids) {
        pluginsclassifyService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}